﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace WebApplication4
{
    public partial class _Default : System.Web.UI.Page
    {
        static bool S_press = false; // use to check when presss + - x : for clear content in textbox
        static float S_M = 0;
        public enum PHEPTINH
        {
            KHONG,
            CONG,
            TRU,
            NHAN,
            CHIA
        }
        static float S_num_before = 0; // num of before position in pheptinh
        static PHEPTINH S_pheptinh = PHEPTINH.KHONG; 


        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void _Clear()
        {
            if (S_press == true)
            {
                this.TextBox2.Text = "0";
                S_press = false;
            }
        }

        protected void _Display(string str)
        {
            if (str != ".")
            {
                this.TextBox2.Text += str;
                this.TextBox2.Text =
                    (float.Parse(this.TextBox2.Text)).ToString();
            }
            if (str == ".")
                this.TextBox2.Text += str;
        }

        protected void Button_C_Click(object sender, EventArgs e)
        {
            this.TextBox2.Text = "0";
        }

        protected void Button_0_Click(object sender, EventArgs e)
        {
            this._Clear();
            this._Display("0");
        }

        protected void Button_1_Click(object sender, EventArgs e)
        {
            this._Clear();
            this._Display("1");
        }

        protected void Button_2_Click(object sender, EventArgs e)
        {
            this._Clear();
            this._Display("2");
        }

        protected void Button_3_Click(object sender, EventArgs e)
        {
            this._Clear();
            this._Display("3");
        }

        protected void Button_4_Click(object sender, EventArgs e)
        {
            this._Clear();
            this._Display("4");
        }

        protected void Button_5_Click(object sender, EventArgs e)
        {
            this._Clear();
            this._Display("5");
        }

        protected void Button_6_Click(object sender, EventArgs e)
        {
            this._Clear();
            this._Display("6");
        }

        protected void Button_7_Click(object sender, EventArgs e)
        {
            this._Clear();
            this._Display("7");
        }

        protected void Button_8_Click(object sender, EventArgs e)
        {
            this._Clear();
            this._Display("8");
        }

        protected void Button_9_Click(object sender, EventArgs e)
        {
            this._Clear();
            this._Display("9");
        }

        protected void Button_cham_Click(object sender, EventArgs e)
        {
            this._Clear();
            this._Display(".");
        }

        protected void Button_add_Click(object sender, EventArgs e)
        {
            S_pheptinh = PHEPTINH.CONG;
            S_press = true;
            S_num_before = float.Parse(this.TextBox2.Text);
        }

        protected void Button_sub_Click(object sender, EventArgs e)
        {
            S_pheptinh = PHEPTINH.TRU;
            S_press = true;
            S_num_before = float.Parse(this.TextBox2.Text);
        }

        protected void Button_mul_Click(object sender, EventArgs e)
        {
            S_pheptinh = PHEPTINH.NHAN;
            S_press = true;
            S_num_before = float.Parse(this.TextBox2.Text);
        }

        protected void Button_div_Click(object sender, EventArgs e)
        {
            S_pheptinh = PHEPTINH.CHIA;
            S_press = true;
            S_num_before = float.Parse(this.TextBox2.Text);
        }

        protected void Button_M_Click(object sender, EventArgs e)
        {
            this.TextBox2.Text = S_M.ToString();
        }

        protected void Button_Madd_Click(object sender, EventArgs e)
        {
            S_M += float.Parse(this.TextBox2.Text);
        }

        protected void Button_Msub_Click(object sender, EventArgs e)
        {
            S_M -= float.Parse(this.TextBox2.Text);
        }

        protected void Button_equal_Click(object sender, EventArgs e)
        {
            float result = new float();
            float num_after = float.Parse(this.TextBox2.Text);
            switch (S_pheptinh)
            {
                case PHEPTINH.CONG:
                    result = S_num_before + num_after;
                    break;
                case PHEPTINH.TRU:
                    result = S_num_before - num_after;
                    break;
                case PHEPTINH.NHAN:
                    result = S_num_before * num_after;
                    break;
                case PHEPTINH.CHIA:
                    result = S_num_before / num_after;
                    break;
                default:
                    break;
            }
            S_pheptinh = PHEPTINH.KHONG;
            this.TextBox2.Text = result.ToString();
            S_press = true;
        }
    }
}
